<template>
	<view class="con">
		<!-- 头部组件 -->
		<!-- <address-head class="address-head-class"></address-head> -->
		<!-- 地址组件 -->
		<address-list :addressList="addressListData" @changeRadio="changeRadio" @removeAddressById="removeAddressById"
			@updateAddressById="updateAddressById" @choiceAddress="choiceAddress">
		</address-list>

		<!-- 新增地址按钮 -->
		<view class="bottom-zw"></view>
		<view class="bottom">
			<view class="btn" @click="gotoAddAddress">
				<text>添加地址</text>
			</view>
		</view>

	</view>
</template>

<script>
	import addressList from './address-child/address-list.vue'

	import {
		getShopAddress,
		updateAddressIsDefault,
		deleteById
	} from '@/api/http/main/market/address.js'

	export default {
		components: {
			addressList
		},
		data() {
			return {
				addressListData: [],
				userId: uni.getStorageSync('userId')
			}
		},
		methods: {
			//获取所有地址信息
			getAddress() {
				//console.log('更改信息')
				getShopAddress().then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					//console.log(res.data)
					this.addressListData = res.data.busInfo
				})
			},
			//添加地址
			gotoAddAddress() {
				uni.navigateTo({
					url: '/subpkg/main/my/address/add-address/add-address'
				})
			},
			//修改地址默认状态
			changeRadio(e) {
				updateAddressIsDefault(e.addressId, this.userId).then(res => {
					if (res.data.pubInfo.code !== 0) {
						uni.$showMsg(res.data.pubInfo.message);
					}
					//查询状态
					this.getAddress()
				})
			},
			//删除指定地址信息
			async removeAddressById(addressId) {		
				var that = this
				uni.$isShowModal('是否删除地址信息？').then(resultState => {
					if (resultState) {
						deleteById(addressId).then(res => {
							if (res.data.pubInfo.code !== 0) {
								uni.$showMsg(res.data.pubInfo.message);
							}
							//查询状态
							that.getAddress()
						})
					}
				})
			},
			
			
			//修改指定的地址信息
			updateAddressById(item) {
				uni.navigateTo({
					url: '/subpkg/main/my/address/update-address/update-address?updateAddressId= ' + item.addressId
				})
			},
			//选择指定地址
			choiceAddress(item) {
				// updateAddressIsDefault(item.addressId, this.userId).then(res => {
				// 	if (res.data.pubInfo.code !== 0) {
				// 		return uni.$showMsg(res.data.pubInfo.message);
				// 	}
				// 	this.goBack()
				// })
			},
			//返回上一页 ，并执行指定方法
			goBack() {
				// var pages = getCurrentPages(); //当前页面栈  
				// let beforePage = pages[pages.length - 2]; //获取上一个页面实例对象  
				// beforePage.$vm.getAddress(); //触发上一个页面中的getUserInfo方法
				// //uni.navigateBack();

				// uni.navigateBack({
				// 	delta: 1, //返回层数，2则上上页
				// })
			}
		},
		created() {
			this.getAddress()
		},
	}
</script>

<style lang="scss" scoped>
	.con {
		width: 100%;
		height: 100vh;
		background: #F3F3F3;
	}

	.bottom-zw {
		width: 100%;
		height: 130rpx;
	}

	.bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 130rpx;
		background: #fff;

		.btn {
			margin-top: 10rpx;
			width: 90%;
			margin-left: 5%;
			height: 80rpx;
			background: rgba(10, 163, 71, 1);
			text-align: center;
			line-height: 80rpx;
			border-radius: 40rpx;

			text {
				font-size: 30rpx;
				font-weight: 400;
				color: rgba(255, 255, 255, 1);
			}

		}
	}
</style>
